<template>
  <div class="login-container" @keydown.enter="Loginbtn">
    <el-form
      v-if="pwdlogin"
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="animated zoomInDown login-form"
      auto-complete="on"
      label-position="left"> 

      <div class="title-container" style="padding-bottom:30px; color:white;font-size: 20px;">
        我也不知道为什么  劳驾先登个录吧
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
        <el-input
          :autofocus="true"
          ref="username"
          v-model="loginForm.username"
          placeholder="请输入用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"/>
      </el-form-item>
      <el-form-item prop="pwd">
        <span class="svg-container">
          <svg-icon icon-class="password"/>
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.pwd"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"/>
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
      </el-form-item>
      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="Loginbtn">登录
      </el-button>
    </el-form>
    <el-form
      v-if="!pwdlogin"
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="animated zoomInUp login-form"
      auto-complete="on"
      label-position="left"> 
      <div class="title-container">
        <h3 class="title">book</h3>
      </div>
      <div id="logincode" ref="logincode"></div>
      <img :src="src" alt=""/>
    </el-form> 
  </div>
</template>

<script>
import Cookies from "js-cookie";
import "../../styles/Animate.css";
import QRCode from "qrcode";
import websocket from "../../components/websocket";

export default {
  name: "Login",
  data() {
    return {
      superlist: ["SuperAdmin"],
      src: "",
      pwdlogin: true,
      loginForm: {
        username: "",
        pwd: "",
      },
      loginRules: {
        username: [
          {required: true, trigger: "blur", message: "请输入用户名"},
        ],
        pwd: [{required: true, trigger: "blur", message: "请输入密码"}],
      },
      loading: false,
      passwordType: "password",
      dialog: false,
      saledialog: false,
      saleform: {},
      warehouse_alert: [],
    };
  },
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    }, 
    randomString(len) {
      len = len || 32;
      var $chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
      var maxPos = $chars.length;
      var pwd = "";
      for (var i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
      }
      return pwd;
    }, 
    getquanxian(next_url) {
      this.$get(this.$api + "/System/Role/GetMyMenu").then((res) => {
        localStorage.setItem("router", JSON.stringify(res.data));
        this.$router.push({
          path: next_url,
        });
      });
    },
    Loginbtn() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$post(this.$api + "/Login.do", {
            username: this.loginForm.username,
            pwd: this.loginForm.pwd,
          }).then((res) => {
            this.loading = false;
            if (res.code === 0) {
              Cookies.set("wx_name", res.data.wx_name);
              Cookies.set("crmtoken", res.data.token);
              Cookies.set("role_id", res.data.role_id);
              Cookies.set("headimg", res.data.headimg);
              Cookies.set("uid", res.data.uid); 
              var next_url = res.data.role_id=='1728776833970880512'?"/niuwan/statistics":"/"
              this.getquanxian(next_url);
            } else {
              this.$notify({
                title: "提示",
                message: res.msg,
                type: "error",
              });
            }
          });
        }
      });
    },
  },
};
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0;
      -webkit-appearance: none;
      border-radius: 0;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #444a63;
$dark_gray: #728ca4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    background-color: #3a4677;
    border-radius: 4%;
    position: relative;
    width: 520px;
    max-width: 90%;
    padding: 65px 35px;
    margin: 160px auto 0;
    overflow: hidden;
    text-align: center;
    -moz-box-shadow: 10px 10px 5px #101c2c; /* 老的 Firefox */
    box-shadow: 10px 10px 5px #101c2c;

    .QRcode {
      position: absolute;
      right: 30px;
      top: 25px;
      cursor: pointer;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0 auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

.dialogdiv {
  width: 50%;
  margin: 0 auto 80px;

  &:last-child {
    margin: 0 auto;
  }
}
</style>
